﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MindBreaker.LevelManagement;

namespace MindBreaker.GameObjects
{
    public class PhysicEngine
    {
        /**************************/
        /** SINGLETON MANAGEMENT **/
        /**************************/
        private static PhysicEngine instance;

        private PhysicEngine()
        {
            Console.WriteLine("Création du PhysicEngine...");
            this._objectList = new List<GameObject>();
            this.Reference = new List<PhysicObject>();
        }

        public static PhysicEngine getInstance()
        {
            if (instance == null)
                instance = new PhysicEngine();
            return instance;
        }

        public static void destroyInstance()
        {
            instance = null;
        }

        /****************/
        /** ATTRIBUTES **/
        /****************/

        private List<GameObject> _objectList;
        private List<PhysicObject> _reference;

        public void add(GameObject obj)
        {
            this.ObjectList.Add(obj);
        }

        public void update()
        {
            int collision = 0, collisionCounter;
            foreach (PhysicObject reference in this.Reference) {
                collisionCounter = 0;
                foreach (GameObject obj in this.ObjectList) {
                    if (!obj.Died) {
                        if (obj != reference) {
                            collision = reference.collisionWith(obj);

                            if (collision != 0) {
                                collisionCounter++;
                                //Gestion vitesse balle
                                if (collision == 3) {
                                    reference.reverseSpeedX();
                                    reference.reverseSpeedY();
                                } else if (collision < 0) {
                                    reference.reverseSpeedY();
                                } else {
                                    reference.reverseSpeedX();
                                }
                                // Destruction des cases
                                if (obj is LevelCase)
                                    obj.DestroyGameObject();
                            }
                        }
                    }
                }
                if (collisionCounter == 0) {
                    reference.LockCollision = (reference.LockCollision == 0) ? 0 : reference.LockCollision - 1;
                }
            }
        }


        /****************/
        /** PROPERTIES **/
        /****************/

        public List<GameObject> ObjectList
        {
            get { return this._objectList; }
        }

        public List<PhysicObject> Reference
        {
            get { return this._reference; }
            set { this._reference = value; }
        }
    }
}
